package Game;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.RelativeMouse;
import com.jme.input.action.KeyNodeBackwardAction;
import com.jme.input.action.KeyNodeForwardAction;
import com.jme.input.action.KeyNodeLookDownAction;
import com.jme.input.action.KeyNodeLookUpAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.input.action.KeyNodeStrafeLeftAction;
import com.jme.input.action.KeyNodeStrafeRightAction;
import com.jme.input.action.NodeMouseLook;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.CameraNode;
import com.jme.scene.Spatial;
import javax.swing.JProgressBar;

public class CustomFPSInputHandler extends InputHandler {

    private Camera cam;
    private FPSPlayer player;
    private float moveSpeed;
    private float turnSpeed;
    private static JProgressBar condition = new JProgressBar(0,50);

    public CustomFPSInputHandler(Camera cam, float moveSpeed, float turnSpeed, FPSPlayer player) {
        CameraNode camNode = new CameraNode("camera", cam);
        camNode.setLocalTranslation(1, 2.5f, -7);
        player.attachChild(camNode);
        this.cam = cam;
        this.player = player;
        //this.moveSpeed = moveSpeed;
        //this.turnSpeed = turnSpeed;

        setKeyBindings();
        setUpMouse(player,turnSpeed);
        setActions(player,moveSpeed,turnSpeed);
    }

    @Override
    public void update(float time) {
        super.update(time);
        
    }
    public float[] getCamDirection(){
        float[] components = new float[3];
        components[0] = cam.getDirection().x;
        components[1] = cam.getDirection().y;
        components[2] = cam.getDirection().z;
        return components;
    }
    public float[] getPlayerLocation(){
        float[] componentsWeapons = new float[3];
        componentsWeapons[0] = player.getLocalTranslation().x;
        componentsWeapons[1] = player.getLocalTranslation().y;
        componentsWeapons[2] = player.getLocalTranslation().z;
        return componentsWeapons;
    }
    public void performActions(int code){
        
    }
    public static int getCondition(){
        return condition.getValue();
    }
    public static void setCondition(int cond){
        condition.setValue(cond);
    }

    private void setKeyBindings() {
        KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

        keyboard.set("forward", KeyInput.KEY_W);
        keyboard.set("backward", KeyInput.KEY_S);
        keyboard.set("strafeLeft", KeyInput.KEY_A);
        keyboard.set("strafeRight", KeyInput.KEY_D);
        keyboard.set("lookUp", KeyInput.KEY_UP);
        keyboard.set("lookDown", KeyInput.KEY_DOWN);
        keyboard.set("turnRight", KeyInput.KEY_RIGHT);
        keyboard.set("turnLeft", KeyInput.KEY_LEFT);
        keyboard.set("run", KeyInput.KEY_SPACE);
    }

    private void setUpMouse( Spatial node, float mouseSpeed ) {
        RelativeMouse mouse = new RelativeMouse("Mouse Input");
        mouse.registerWithInputHandler( this );

        NodeMouseLook mouseLook = new NodeMouseLook(mouse, node, 0.1f);
        mouseLook.setSpeed( mouseSpeed );
        mouseLook.setLockAxis(new Vector3f(node.getLocalRotation().getRotationColumn(1).x,
                node.getLocalRotation().getRotationColumn(1).y,
                node.getLocalRotation().getRotationColumn(1).z));
        addAction(mouseLook);
    }

    private void setActions( Spatial node, float moveSpeed, float turnSpeed ) {
        addAction( new KeyNodeForwardAction( node, moveSpeed ), "forward", true );
        addAction( new KeyNodeBackwardAction( node, moveSpeed ), "backward", true );
        addAction( new KeyNodeStrafeLeftAction( node, moveSpeed ), "strafeLeft", true );
        addAction( new KeyNodeStrafeRightAction( node, moveSpeed ), "strafeRight", true );
        addAction( new KeyNodeLookUpAction( node, turnSpeed ), "lookUp", true );
        addAction( new KeyNodeLookDownAction( node, turnSpeed ), "lookDown", true );
        KeyNodeRotateRightAction rotateRight = new KeyNodeRotateRightAction( node, turnSpeed );
        rotateRight.setLockAxis( node.getLocalRotation().getRotationColumn( 1 ) );
        addAction( rotateRight, "turnRight", true );
        KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction( node, turnSpeed );
        rotateLeft.setLockAxis( node.getLocalRotation().getRotationColumn( 1 ) );
        addAction( rotateLeft, "turnLeft", true );
        KeyNodeRunAction run = new KeyNodeRunAction(player, 70);
        addAction(run,"run",true);
    }

}
